<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户编码" prop="code">
      <el-input v-model="dataForm.code" placeholder="用户编码"></el-input>
    </el-form-item>
    <el-form-item label="用户名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="用户名称"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="新建时间" prop="creationtime">
      <el-input v-model="dataForm.creationtime" placeholder="新建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="modifytime">
      <el-input v-model="dataForm.modifytime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="dr" prop="dr">
      <el-input v-model="dataForm.dr" placeholder="dr"></el-input>
    </el-form-item>
    <el-form-item label="时间戳" prop="ts">
      <el-input v-model="dataForm.ts" placeholder="时间戳"></el-input>
    </el-form-item>
    <el-form-item label="预留01" prop="def01">
      <el-input v-model="dataForm.def01" placeholder="预留01"></el-input>
    </el-form-item>
    <el-form-item label="预留02" prop="def02">
      <el-input v-model="dataForm.def02" placeholder="预留02"></el-input>
    </el-form-item>
    <el-form-item label="预留03" prop="def03">
      <el-input v-model="dataForm.def03" placeholder="预留03"></el-input>
    </el-form-item>
    <el-form-item label="预留04" prop="def04">
      <el-input v-model="dataForm.def04" placeholder="预留04"></el-input>
    </el-form-item>
    <el-form-item label="预留05" prop="def05">
      <el-input v-model="dataForm.def05" placeholder="预留05"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: '',

          name: '',

          password: '',

          creationtime: '',

          modifytime: '',

          dr: '',

          ts: '',

          def01: '',

          def02: '',

          def03: '',

          def04: '',

          def05: '',

        },
        dataRule: {
          code: [
            { required: true, message: '用户编码不能为空', trigger: 'blur' }
          ],

          name: [
            { required: true, message: '用户名称不能为空', trigger: 'blur' }
          ],

          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],

          creationtime: [
            { required: true, message: '新建时间不能为空', trigger: 'blur' }
          ],

          modifytime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],

          dr: [
            { required: true, message: 'dr不能为空', trigger: 'blur' }
          ],

          ts: [
            { required: true, message: '时间戳不能为空', trigger: 'blur' }
          ],

          def01: [
            { required: true, message: '预留01不能为空', trigger: 'blur' }
          ],

          def02: [
            { required: true, message: '预留02不能为空', trigger: 'blur' }
          ],

          def03: [
            { required: true, message: '预留03不能为空', trigger: 'blur' }
          ],

          def04: [
            { required: true, message: '预留04不能为空', trigger: 'blur' }
          ],

          def05: [
            { required: true, message: '预留05不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/root/admins/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.admins.code
                this.dataForm.name = data.admins.name
                this.dataForm.password = data.admins.password
                this.dataForm.creationtime = data.admins.creationtime
                this.dataForm.modifytime = data.admins.modifytime
                this.dataForm.dr = data.admins.dr
                this.dataForm.ts = data.admins.ts
                this.dataForm.def01 = data.admins.def01
                this.dataForm.def02 = data.admins.def02
                this.dataForm.def03 = data.admins.def03
                this.dataForm.def04 = data.admins.def04
                this.dataForm.def05 = data.admins.def05
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/root/admins/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,

                'name': this.dataForm.name,

                'password': this.dataForm.password,

                'creationtime': this.dataForm.creationtime,

                'modifytime': this.dataForm.modifytime,

                'dr': this.dataForm.dr,

                'ts': this.dataForm.ts,

                'def01': this.dataForm.def01,

                'def02': this.dataForm.def02,

                'def03': this.dataForm.def03,

                'def04': this.dataForm.def04,

                'def05': this.dataForm.def05,

              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
